package com.blog.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.databind.util.JSONPObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 



import com.alibaba.fastjson.JSON;
import com.blog.model.ArticleWithBLOBs;
import com.blog.service.interfaces.IarticleService;
 

@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	IarticleService articleService;
	@Autowired
	ArticleWithBLOBs articleWithBLOBs;
	
	
	/*  旧程序段  起   */
	//接口 使用
	@RequestMapping("/addArticle")
	@ResponseBody
	public Map<String,String> addArticle(String title,String content,String category,@RequestParam(required = false, defaultValue = "/images/cover.jpg")String cover,@RequestParam(required = false) String password,@RequestParam(required = false) String brief){
		articleWithBLOBs.setId(null);
		articleWithBLOBs.setTitle(title);
		articleWithBLOBs.setContent(content);
		articleWithBLOBs.setCover(cover);
		if(category==""){category="默认分类";}
		articleWithBLOBs.setPublishTime((int) (System.currentTimeMillis()/1000));
		articleWithBLOBs.setPopularPoint(100);
		articleWithBLOBs.setCategory(category);
		articleWithBLOBs.setBrief(brief);
		if(password!=null && password!=""){articleWithBLOBs.setPassword(Integer.valueOf(password));}else{articleWithBLOBs.setPassword(null);}
		//articleWithBLOBs.setPassword(Integer.valueOf(11));
		System.out.println(articleService.addArticleTodatabase(articleWithBLOBs));
		Map<String,String> resultMap = new HashMap<String, String>(); 
		resultMap.put("result","发布成功");
		return resultMap;
	}
	

	//接口 废弃
	@RequestMapping("/getArticleById")
	@ResponseBody
	public ArticleWithBLOBs getArticleById(@RequestParam(required = false, defaultValue = "1")String id){
	 	//List<ArticleWithBLOBs> articleList=new ArrayList<ArticleWithBLOBs>();
		System.out.println("得到特定文章");
 
	 	//articleList.addAll();
		return articleService.getArticleById(id);
	}
	
	//接口 废弃
	@RequestMapping("/getPageIdList")
	@ResponseBody
	public Map<String,String> getPageIdList(@RequestParam(required = false, defaultValue = "1")String page){
		System.out.println("计算页码");

		return articleService.getPageList(page);
	}
	
	//del 接口 使用
	@RequestMapping("/delArticle")
	@ResponseBody
	public int delArticle(String id){
		
		return articleService.delArticle(id);
	}
	
	//update id要有
	//接口 使用
	@RequestMapping("/updateArticle")
	@ResponseBody
	public Map<String,String> updateArticle(String id,String title,String content,String category,@RequestParam(required = false, defaultValue = "http://7xkz5s.com1.z0.glb.clouddn.com/timg.jpg")String cover,@RequestParam(required = false) String password,@RequestParam(required = false) String brief){
		articleWithBLOBs.setId(Integer.valueOf(id));
		articleWithBLOBs.setTitle(title);
		articleWithBLOBs.setContent(content);
		articleWithBLOBs.setCover(cover);
		articleWithBLOBs.setBrief(brief);
		if(category==""){category="默认分类";}
		articleWithBLOBs.setCategory(category);
		if(password!=null && password!=""){articleWithBLOBs.setPassword(Integer.valueOf(password));}else{articleWithBLOBs.setPassword(null);}
		System.out.println(articleService.updateArticleTodatabase(articleWithBLOBs));
		Map<String,String> resultMap = new HashMap<String, String>(); 
		resultMap.put("result","改成功");
		return resultMap;
	}
	
	//like dislike
	//接口 使用
	@RequestMapping("/like")
	@ResponseBody
	public int like(String code,String id){
		//有用 code 1 +1  喜欢code 2 +2  不喜欢 code 3 -1
		int point=1;
		if(code.equals("1")){point=1;}else if(code.equals("2")){point=2;}else{point=-1;}
		System.out.println("计算评分");
		System.out.println("评分是"+point);
		return articleService.increasePoint(point,id);
	}
	
	//接口
	@RequestMapping("/getpopularlist")
	@ResponseBody
	public List<ArticleWithBLOBs> getpopularlist(){
		return articleService.getpopularFromdatabase();
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// 接口 废弃
	@RequestMapping("/getCategory")
	@ResponseBody
	public List<ArticleWithBLOBs> getCategory(){
		return articleService.getCategory();
	}
	
	
	//接口 测试 地址/article/getjson3
	@RequestMapping(value="/getjson3")
	@ResponseBody
	public JSONPObject getjson3(@RequestParam(required=false) String callback){
		System.out.println("访问了3");
		Map<String,String> resultMap = new HashMap<String, String>(); 
			resultMap.put("result", "得到了json"); 
			resultMap.put("result", "得到了jso2"); 
	        //return  "JP="+JSON.toJSONString(resultMap);  
		    if(callback==null){return new JSONPObject("default", "10");}
			return new JSONPObject(callback, resultMap); 
	}
	

	/*  旧程序段  止   */
	
	
	
	
	@RequestMapping("/sitemap")
	public String sitmap(Model model){
		model.addAttribute("article",articleService.getAllArticle());
		return "sitemap";
	}
	
	@RequestMapping("/out")
	@ResponseBody
	public JSONPObject out(Model model,HttpServletRequest request){
		
		//来自指定ua可以显示密码
		if("0430".equals(request.getHeader("USER-AGENT"))){
			return new JSONPObject("001",articleService.getAllArticle());
		}else{
			List<ArticleWithBLOBs> articles=articleService.getAllArticle();
			for(ArticleWithBLOBs a:articles){
				a.setPassword(null);//在页面上不能明文显示密码
			}
			return new JSONPObject("001",articles);
		}
	}
	
	
	public static void main(String[] args){
		//System.out.println("<img src=\"http://7xkz5s.com1.z0.glb.clouddn.com/uuid_299944796cca498480e51b7e43eaf938.jpg\" alt=\"\">".replace("\"",""));	
		
		
	}
	
	
	
 
}
